package cn.scu.ne04jdemo.controller;

import cn.scu.ne04jdemo.entitiy.ScheDevInfoEntity;
import cn.scu.ne04jdemo.entitiy.ScheDevTypeInfoEntity;
import cn.scu.ne04jdemo.entitiy.ScheProdTaskInfoEntity;
import cn.scu.ne04jdemo.service.ScheProdTaskInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sche/prod/task/info")
public class ScheProdTaskInfoController {

    @Autowired
    private ScheProdTaskInfoService scheProdTaskInfoService;

    @GetMapping("/findPage")
    public ResponseEntity<IPage> findPage(@RequestParam(required = false) String prodTypeId,
                                          @RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize) {
        LambdaQueryWrapper<ScheProdTaskInfoEntity> queryWrapper = new QueryWrapper<ScheProdTaskInfoEntity>().lambda()
                .eq(StringUtils.isNotBlank(prodTypeId), ScheProdTaskInfoEntity::getProdTypeId, prodTypeId);
        IPage<ScheProdTaskInfoEntity> page = scheProdTaskInfoService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return ResponseEntity.ok(page);
    }

    @PostMapping("/delById")
    public Boolean delById(@RequestBody ScheProdTaskInfoEntity scheProdTaskInfoEntity) {
        try {
            if(ObjectUtils.isNotNull(scheProdTaskInfoEntity)){
                String id = scheProdTaskInfoEntity.getId();
                scheProdTaskInfoService.removeById(id);
            }else{
                throw new Exception();
            }

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @PostMapping("/insert")
    @ResponseBody
    public Boolean insert(@RequestBody ScheProdTaskInfoEntity scheProdTaskInfoEntity){
        try {
            return this.scheProdTaskInfoService.insert(scheProdTaskInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public Boolean update(@RequestBody ScheProdTaskInfoEntity scheProdTaskInfoEntity){
        try {
            scheProdTaskInfoService.updateById(scheProdTaskInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
